import { wxRequest } from '../../utils/server.js';  
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {  
    pageNo:1,
     list:[], 
     imgUrl: app.globalData.imgUrl,
     uploadUrl: wx.getStorageSync('uploadUrl'),
     correct:0
  },

  onLoad(option){
    if(option.typeId){
      this.data.typeId=option.typeId
    }

    if(option.bannertype){
      this.data.bannertype=option.bannertype
      this.getAdPicture()
    }

   if(option.correct==1){
     this.setData({
      correct:option.correct
     })
     this.getDeviceList()
   }


  },
   
  onShow: function (options) {  
    this.list()
  },

   
//获取设备列表
list:function(){       
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/dyeColor/findList',
      method: 'POST',
      data:{ 
        level: 3,
        model: 1, 
        pageNo: this.data.pageNo, 
        pageSize:20,
        typeId:this.data.typeId 
      } 
    }, (res) => {  
      let list = res.list;
      list.map(item=>{
        if(item.videoJson){
          item.videoJson=JSON.parse(item.videoJson)
        }
      })
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }
      this.setData({
        list: list, 
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

      console.log(list)
      
    }) 
  },
  
  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //去染色
  toColor:function(e){
    var itemColor= e.currentTarget.dataset.item
    this.setData({
      itemColor
    },()=>{
      wx.navigateTo({
        url: '/pages/dye_paste/operation', 
        success:(res)=>{
          res.eventChannel.emit('colorItem',itemColor)
        }
      })
    }) 
  },


  toChooseColor:function(e){
    var url=e.currentTarget.dataset.url
    wx.navigateTo({
      url:url+'?typeId='+this.data.typeId   //'/pages/choose_colors/list',
    })
  },



  toDetail:function(e){

    var item=e.currentTarget.dataset.item
    var id = e.currentTarget.dataset.id 
    var videoContextPrev = wx.createVideoContext('index' + id)
    videoContextPrev.stop(); 
    if(item.model==1){ 
      wx.navigateTo({
        url:'/pages/choose_colors/model',
        success:(res)=>{
          res.eventChannel.emit('colorItem',item)
        }
      })
    }else{
      wx.navigateTo({
        url:'/pages/dye_paste/operation'
      })
    }
  },


getAdPicture(){
  _wxRequest.wxTonkenAjax({
    url: app.globalData.apiUrl + '/sys/ad/findAdPicture',
    method: 'POST',
    data:{ 
      keyword: '',
      typeId:this.data.bannertype 
    } 
  }, (res) => {  
    this.setData({
      adlist:res.list
    })
    console.log(res,'11111111111')
  })
},



balanceHandle:function(){ 
  wx.navigateTo({
    url: '/pages/dye_paste/balance?typeId='+ this.data.devList[0].id,
  })
},

 
//获取设备列表
 getDeviceList: function() {
    var params = { 
      pageNo: 1,
      pageSize:100,
      correct:1,
      typeId:this.data.typeId 
    }
     
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findList',
      method: 'POST',
      data:params
    }, (res) => {
      let list = res.list; 
      if(list.length>1){
        this.setData({
          mDevState:true
        })
      } 
      this.setData({        
        devList:list
      })
 
    })
  },


  bindPickerChange:function(e){
    var index=e.detail.value
    wx.navigateTo({
      url: '/pages/dye_paste/balance?typeId='+ this.data.devList[index].id,
    })
  }




})